/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package submarinesControl;

import java.util.Scanner;
import model.Board;
import model.Submarine;
import xml.XmlParser;

/**
 *
 * @author Karnit Genchay
 */
public class SubmarinesControl {
        public Player[] players ;
        private Player[] inActivePlayers ;
        private int numOfPlayers;
        private int numOfRegisteredPlayers;
        private int numberOfCompPlayers;
        private boolean isGameCreated;

        private Board [] boards;
        private XmlParser xml;
        public GameManager game;
        private int currPlayer;
        private boolean gameStarted;



    public SubmarinesControl()
    {  
        xml = new XmlParser();
//        numOfPlayers = xml.getNumOf("player");
        numOfPlayers=0;
        players  =null;
        boards = null;               
        game = null;
        currPlayer=0;
        gameStarted=false;
        isGameCreated = false;
//        players  =new  Player[numOfPlayers];
//        boards = new Board[numOfPlayers];       
//        
//        setPlayers(false);
//        setPlayersBoards();
//        
//        game = new GameManager(numOfPlayers,players,inActivePlayers,boards);
    }
    
    public void updateSubmarinesData(Submarine []s){
        for (int i = 0; i < s.length; i++) {
            String name = xml.getAttribute("boat","name",i);
            String size = xml.getAttribute("boat","size",i);
            int subSize = Integer.parseInt(size);
            s[i] = new Submarine(subSize,name,"","");
//            String name = xml.getAttribute("boat","name",i);
//            String size = xml.getAttribute("boat","size",i);
//            int subSize = Integer.parseInt(size);
//            s[i].setName(name);
//            s[i].setSize(subSize);
        }
    }
    
    public int getNumOfPlayers() {
        return numOfPlayers;
    }
    
    public boolean isIsGameCreated() {
        return isGameCreated;
    }

    public int getNumberOfCompPlayers() {
        return numberOfCompPlayers;
    }

    public int getNumOfRegisteredPlayers() {
        return numOfRegisteredPlayers;
    }

    public void setNumOfRegisteredPlayers(int numOfRegisteredPlayers) {
        this.numOfRegisteredPlayers = numOfRegisteredPlayers;
    }

    public boolean isGameStarted() {
        return gameStarted;
    }

    public int getCurrPlayer() {
        return currPlayer;
    }
    public void setCurrPlayer(int currPlayer) {
        this.currPlayer = currPlayer;
    }

    public void setGameStarted(boolean gameStarted) {
        this.gameStarted = gameStarted;
    }
    public void playGame(){
          game  = new GameManager(numOfPlayers,players,inActivePlayers,boards);
//          game.runGame();
//          gameOverMenu();
    }
     private void gameOverMenu(){
         int choice;
          Scanner in = new Scanner(System.in);
         
         System.out.println("Please choose:");
         System.out.println("1. A new game with the same initial data");
         System.out.println("2. A new game with new initial data");
         System.out.println("3. Quit game");
         choice = in.nextInt();
                 
    switch (choice) {
        case 1:       
            playGame();
            break;
        case 2:
            changeInitialData();
            playGame();
            break;
        case 3:
            return;

        default:
            throw new AssertionError();
    }
         
     }
     
     private void changeInitialData(){
          Scanner in = new Scanner(System.in);
          int choice;
          
         System.out.println("Please choose:");
         System.out.println("1. Change players");
         System.out.println("2. Change initial boards and submarines");
         System.out.println("3. Start game");
         choice = in.nextInt();
                 
    switch (choice) {
        case 1:       
            changePlayers();
            break;
        case 2:
            changeBoards();
            break;
        case 3:
            return;

        default:
            throw new AssertionError();
    }
    changeInitialData();
     }
     private void changePlayers(){
          Scanner in = new Scanner(System.in);
          int  choice;
          
          printPlayers();
          System.out.println("Choose:");
          System.out.println("1. Add player");
          System.out.println("2. Delete player");
          System.out.println("3. Back to change data menu");
            choice = in.nextInt();
                 
    switch (choice) {
        case 1:       
            insertNewPlayer();
            break;
        case 2:
            deletePlayer(choosePlayer());
            break;
        case 3:
            return;

        default:
            throw new AssertionError();
    }       
      changePlayers();    
     }
     private void insertNewPlayer(){
         Scanner in = new Scanner(System.in);
         String name,type;
         System.out.println("name:");
         name= in.next();
         System.out.println("type:");
         type=  in.next();
         
           if (type.equals("HUMAN") || type.equals("COMPUTER")){
                    players[numOfPlayers] =new Player(name,type) ;  
                    boards[numOfPlayers]=new Board(xml.getSubmarineTypesSize());
                    players[numOfPlayers].setBoard(boards[numOfPlayers]);
           } 
            else    //neither
                    throw new IllegalStateException("Neither HUMAN nor COMPUTER");
         numOfPlayers++;
     }
     private void deletePlayer(int playerNum){
    if (playerNum != (numOfPlayers-1))
        for (int i=playerNum;i<numOfPlayers-1;i++){
            inActivePlayers[inActivePlayers.length] =new Player(players[i] .getName(),players[i] .getType()) ; 
            inActivePlayers[inActivePlayers.length-1] .setQuit();
            players[i] = players[i+1];
             boards[i] = boards[i+1];
        }           
    numOfPlayers--;
    players[numOfPlayers] =null;
    boards[numOfPlayers]=null;
     }
          private void changeBoards(){
          Scanner in = new Scanner(System.in);
          int  choice,player;
          
          printPlayers();
          System.out.println("1. Choose player to change his board:");
          System.out.println("2. Back to change data menu");

          choice = in.nextInt();
                 
    switch (choice) {
        case 1:       
            player = choosePlayer();
            changeBoard(player);
            break;
        case 2:
            return;

        default:
            throw new AssertionError();
    }    
    changeBoards();    
          
     }
          private void changeBoard(int i){
              int changeSubNum;
              String position, direction;
              Scanner in = new Scanner(System.in);
               int subNum =  boards[i].printSubmarinesAndReturnLength();
               System.out.println("Which submarine to change?");
               changeSubNum = in.nextInt();
               System.out.println("What position?");
               position = in.next();
               System.out.println("What direction?");
               direction=in.next();
                try {
                  boards[i].changeSubmarine(changeSubNum,position,direction);
              } catch (Exception e) {
                    System.out.println("Illigal choice");
                    throw new RuntimeException(e);
              }
          }
    private int choosePlayer(){
      Scanner in = new Scanner(System.in);     
        System.out.println("Choose:");
        for (int i = 0; i < numOfPlayers; i++) {
            System.out.println((i+1)+"." + players[i]);
        }
        return in.nextInt() -1;
    }
    private void setPlayers( boolean loadedGame){
        String name,type;
        
        if(!loadedGame){
            for (int i = 0; i < numOfPlayers-numberOfCompPlayers; i++) 
                players[i] =new Player("","HUMAN") ;
            for (int i =numOfPlayers-numberOfCompPlayers; i < numOfPlayers; i++) {
                name = xml.getAttribute("player","name",i);
                players[i] =new Player(name,"COMPUTER") ;
            }
        }  
    else{   //loaded game 
            try{
              for (int i = 0; i < numOfPlayers; i++) {
                name = xml.getAttribute("player","name",i);
                type =xml.getAttribute("player","type",i);
                if (type.equals("HUMAN") || type.equals("COMPUTER"))
                        players[i] =new Player(name,type) ;    
                else    //neither
                        throw new IllegalStateException("Neither HUMAN nor COMPUTER");
            }
        }catch(Exception e){    //the xml did not match the game demandes
            throw new NullPointerException();
        }
    }
}

    public void printPlayers(){
        
   for (int i = 0; i < numOfPlayers; i++) 
             System.out.println(players[i]);
    }
               
    public void setNumOfPlayers(int num)
    {            
        this.numOfPlayers = num;
    }
               
    private void  setPlayersBoards() {
         boards = xml.buildPlayersBoard();
            for (int i = 0; i < numOfPlayers-numberOfCompPlayers; i++) {    //Human players
                   boards[i]=new Board(xml.getSubmarineTypesSize());
                   updateSubmarinesData(boards[i].getSubmarines());
                   boards[i].setRandomSubs();
                   players[i].setBoard(boards[i]);
            }
            for (int i = numOfPlayers-numberOfCompPlayers; i < numOfPlayers; i++) { //Computer players
                   players[i].setBoard(boards[i]);
        }
    }

    public GameManager getGameManager() {
        return this.game;
    }

    public void createNewGame(int numberOfPlayers, int numberOfCompPlayers, boolean load) {
        if(load){
            this.numOfPlayers=xml.getNumOf("player");
            this.numberOfCompPlayers = xml.getNumOf("computer");
            this.numOfRegisteredPlayers =numOfPlayers-numberOfCompPlayers;
        }
        else{     
            this.numOfPlayers=numberOfPlayers;
            this.numOfRegisteredPlayers = 0;
            this.numberOfCompPlayers = numberOfCompPlayers;    
        }
        
        players  =new  Player[numOfPlayers];
        boards = new Board[numOfPlayers];       
        inActivePlayers = new Player[numOfPlayers];
                
        setPlayers(load);  //false - not a loaded game
        setPlayersBoards();
        
        game = new GameManager(numOfPlayers,players,inActivePlayers,boards);
        isGameCreated = true;
    }
}
